
import requests
import re
import os
import time
import urllib3
from tqdm import tqdm

bz_baseurl = 'http://www.gmbz.org.cn/file/'
save_path = './GMT'
down_gmt_num = 200 # 下载的GMT数量,大于当前标准数量则代表则下载所有标准

def make_valid_filename(filename):
    invalid_chars = r'[^\w\s.-]'
    valid_filename = re.sub(invalid_chars, '_', filename)
    return valid_filename


session = requests.Session()
headers = {
    "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 "
    "(KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1",
}
session.get(url='http://www.gmbz.org.cn/main/bzlb.html', headers=headers)

url = "http://www.gmbz.org.cn/main/normsearch.json"

data = {
    "draw": 1,
    "columns[0][data]": "NORM_ISO_ID",
    "columns[0][name]": "NORM_ISO_ID",
    "columns[0][searchable]": "true",
    "columns[0][orderable]": "true",
    "columns[0][search][value]": "",
    "columns[0][search][regex]": "false",
    "columns[1][data]": "NORM_NAME_C",
    "columns[1][name]": "NORM_NAME_C",
    "columns[1][searchable]": "true",
    "columns[1][orderable]": "true",
    "columns[1][search][value]": "",
    "columns[1][search][regex]": "false",
    "columns[2][data]": "NORM_ZT_NAME",
    "columns[2][name]": "NORM_ZT",
    "columns[2][searchable]": "true",
    "columns[2][orderable]": "true",
    "columns[2][search][value]": "",
    "columns[2][search][regex]": "false",
    "columns[3][data]": "NORM_FLAG_NAME",
    "columns[3][name]": "NORM_FLAG_NAME",
    "columns[3][searchable]": "true",
    "columns[3][orderable]": "true",
    "columns[3][search][value]": "",
    "columns[3][search][regex]": "false",
    "columns[4][data]": "NORM_NAME_E",
    "columns[4][name]": "NORM_NAME_E",
    "columns[4][searchable]": "true",
    "columns[4][orderable]": "true",
    "columns[4][search][value]": "",
    "columns[4][search][regex]": "false",
    "columns[5][data]": "NORM_CO_NAME",
    "columns[5][name]": "NORM_CO_NAME",
    "columns[5][searchable]": "true",
    "columns[5][orderable]": "true",
    "columns[5][search][value]": "",
    "columns[5][search][regex]": "false",
    "columns[6][data]": "NORM_CA_NAME",
    "columns[6][name]": "NORM_CA_NAME",
    "columns[6][searchable]": "true",
    "columns[6][orderable]": "true",
    "columns[6][search][value]": "",
    "columns[6][search][regex]": "false",
    "columns[7][data]": "NORM_PUB_DATE",
    "columns[7][name]": "NORM_PUB_DATE",
    "columns[7][searchable]": "true",
    "columns[7][orderable]": "true",
    "columns[7][search][value]": "",
    "columns[7][search][regex]": "false",
    "columns[8][data]": "NORM_IMP_DATE",
    "columns[8][name]": "NORM_IMP_DATE",
    "columns[8][searchable]": "true",
    "columns[8][orderable]": "true",
    "columns[8][search][value]": "",
    "columns[8][search][regex]": "false",
    "columns[9][data]": "UP_GB_FLAG",
    "columns[9][name]": "UP_GB_FLAG",
    "columns[9][searchable]": "true",
    "columns[9][orderable]": "true",
    "columns[9][search][value]": "",
    "columns[9][search][regex]": "false",
    "columns[10][data]": "10",
    "columns[10][name]": "",
    "columns[10][searchable]": "true",
    "columns[10][orderable]": "true",
    "columns[10][search][value]": "",
    "columns[10][search][regex]": "false",
    "order[0][column]": "0",
    "order[0][dir]": "asc",
    "start": "0",
    "length": down_gmt_num,
    "search[value]": "",
    "search[regex]": "false",
    "norm_iso_id": "",
    "norm_flag": "",
    "norm_name_c": "",
    "norm_name_e": "",
    "norm_co_name": "",
    "norm_zt": "",
    "norm_pub_date_begin": "",
    "norm_pub_date_end": "",
    "norm_imp_date_begin": "",
    "norm_imp_date_end": "",
}

r = session.post(url, data=data, timeout=30)
gmt_data = r.json()

#print(gmt_data)

bz_inf = {}

if gmt_data["code"] != '200':
    print("获取数据数据失败: {}".format(gmt_data["msg"]))
    exit()

os.makedirs(save_path, exist_ok=True)

print('*' * 80)
for item in tqdm(gmt_data["data"]):
    print(item["NORM_ID"] + " " + item["NORM_NAME_C"] + " ")
    
    filename = make_valid_filename(item["NORM_ID"] + " " + item["NORM_NAME_C"])
    if os.path.exists(os.path.join(save_path, filename + ".pdf")):
        print(filename + ".pdf" + " 已存在,跳过...")
        continue
    
    for i in range(3):
        try:
            pdf_data = session.get(url=bz_baseurl + item["NORM_APP_ADDR"])
            with open(os.path.join(save_path, filename + ".pdf"), "wb") as f:
                f.write(pdf_data.content)
            break
        except Exception as e:
            print("出错了:{}，重试{}/3次".format(str(e), i+1))
            time.sleep(10)
            if i == 3:
                raise
    time.sleep(20)